//获取app实例
const app = getApp();

Page({
  data: {
    token: wx.getStorageSync("loginFlag"),
    userInfo: {},
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse("button.open-type.getUserInfo"),
    // 是否登录，根据后台返回的token判断
    hasLogin: wx.getStorageSync("loginFlag") ? true : false,
    useraccount: null,  //学号
    userpwd: null, //密码 
  },
  onLoad(option){
    const this_ = this
    console.log(option)
    this_.setData({
      useraccount: option.useraccount,
      userpwd: option.userpwd
    })


    //登录用户标识
    wx.login({
      success: ({code}) => {
        console.log("code=" + code)
        if(code){
          //发起网络请求
          wx.request({
            url: 'http://localhost:8080/wx/getSeesionId',
            method: 'POST',
            header:{
              'content-type':'application/x-www-form-urlencoded',
            },
            data: {
              code: code
            },
            success(res){
              console.log(res)
              if(res.data.code == 0){
                wx.setStorage({//存储到本地
                  key:'sessionId',
                  data:res.data.data
                })
              }else{
                wx.removeStorage({
                  key: 'sessionId',
                })
              }
            }
          })
        } else {
          console.log('发生错误')
        }
      }
    })
},

//点击了微信一键登录
authLogin(){
  const that = this
  console.log("用户按了允许授权按钮");
  let sessionStorageId = wx.getStorageSync("sessionId")
  wx.getUserProfile({
    desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
    success: (res) => {
      wx.request({
        url: 'http://localhost:8080/jw/login',
        method: 'post',
        data: {
          seesionId:sessionStorageId,
          encryptedData: res.encryptedData,
          iv: res.iv,
          useraccount: that.data.useraccount,
          userpwd: that.data.userpwd,
        },
        success: function(res) {
          // res = res.data
          // wx.setStorageSync("token", res.data.token);
          console.log(res)
          // wx.request({
          //   url: 'http://localhost:8080/wx/userinfo',
          //   method: 'GET',
          //   data: {
          //     token: wx.getStorageSync("token")
          //   },
          //   success:function(res){
          //     that.globalData.userInfo = res.data
          //     wx.setStorageSync("userInfo", res.data);
          //     wx.setStorageSync("loginFlag", wx.getStorageSync("token"));

          //   }
          // })

          wx.setStorageSync("userInfo", res.data.data);
          wx.setStorageSync("loginFlag", wx.getStorageSync("token"));
          wx.setStorageSync("token", res.data.data.token);
          wx.switchTab({
            url:  '/pages/index/index'
          })
        },
        fail: function (error) {
          console.log(error);
          // 获取 userInfo 失败，去检查是否未开启权限
          wx.hideLoading();
          wx.showToast({
            title: '调用request接口失败',
            icon: 'none'
          })
          console.log(error);
          wx.navigateTo({
            url: "/pages/login/index",
          })
        }
      })
    }
  })
},
  

 
})